package com.yequan.sourcecode.javautil;

import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * @author : Administrator
 * @date : 2020/3/18
 */
public class PriorityQueueDemo {


    public static void main(String[] args) {
        PriorityQueueDemo priorityQueueDemo = new PriorityQueueDemo();
//        priorityQueueDemo.priorityInnerComparator();
//        priorityQueueDemo.priorityOuterComparator();
        priorityQueueDemo.priorityDefaultComparator();
    }

    /**
     * 使用内部比较器
     */
    private void priorityInnerComparator() {
        //内部使用最小顶堆实现,优先级数值越小先出队列
//        PriorityQueue<Task> taskPriorityQueue = new PriorityQueue<>(Comparator.comparing(Task::getPriority));
        PriorityQueue<Task> taskPriorityQueue = new PriorityQueue<>((t1, t2) -> t1.getPriority() - t2.getPriority());

        initQueue(taskPriorityQueue);
        while (!taskPriorityQueue.isEmpty()) {
            System.out.println(taskPriorityQueue.poll());
        }

    }

    /**
     * 使用外部比较器
     */
    private void priorityOuterComparator() {
        PriorityQueue<Task> taskPriorityQueue = new PriorityQueue<>(new Comparator<Task>() {
            @Override
            public int compare(Task t1, Task t2) {
                return t2.getPriority() - t1.getPriority();
            }
        });
        initQueue(taskPriorityQueue);
        while (!taskPriorityQueue.isEmpty()) {
            System.out.println(taskPriorityQueue.poll());
        }
    }

    /**
     *
     */
    private void priorityDefaultComparator(){
        PriorityQueue<Task> taskPriorityQueue = new PriorityQueue<>();
        initQueue(taskPriorityQueue);
        while (!taskPriorityQueue.isEmpty()) {
            System.out.println(taskPriorityQueue.poll());
        }
    }
    /**
     * 初始化优先级队列
     *
     * @param taskPriorityQueue
     */
    private void initQueue(PriorityQueue<Task> taskPriorityQueue) {
        taskPriorityQueue.add(new Task(1, "learn java"));
        taskPriorityQueue.add(new Task(3, "learn C#"));
        taskPriorityQueue.add(new Task(4, "learn js"));
        taskPriorityQueue.add(new Task(2, "learn Python"));
        taskPriorityQueue.add(new Task(2, "learn shell"));
        taskPriorityQueue.add(new Task(5, "learn c++"));
    }

    class Task {
        private int priority;
        private String taskName;

        public Task(int priority, String taskName) {
            this.priority = priority;
            this.taskName = taskName;
        }

        public int getPriority() {
            return priority;
        }

        public void setPriority(int priority) {
            this.priority = priority;
        }

        public String getTaskName() {
            return taskName;
        }

        public void setTaskName(String taskName) {
            this.taskName = taskName;
        }

        @Override
        public String toString() {
            return "Task{" +
                    "priority=" + priority +
                    ", taskName='" + taskName + '\'' +
                    '}';
        }
    }

}
